using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public static class BattleInfoMgr
{

	[Serializable]
	public class BattleInfoValue
	{
		public int key;
		public bool value;

		public BattleInfoValue(int key, bool value)
		{
			this.key = key;
			this.value = value;
		}
	}

	[Serializable]
	public class BattleInfoList
	{
		public List<BattleInfoValue> list = new List<BattleInfoValue>();
	}
	public const string BATTLE_FILE = "battle.dat";

	private static string FILE_PATH = "Cfg/Battle";

	private static List<BattleInfo> _battleInfos = new List<BattleInfo>();

	private static Dictionary<int, bool> _lockStatusMap = new Dictionary<int, bool>();

	private static bool _isLoaded = false;

	public static List<BattleInfo> battleInfos => _battleInfos;

	public static void Load()
	{
		if (!_isLoaded)
		{
			_battleInfos = Utils.LoadXML<BattleInfo>(FILE_PATH);
			for (int i = 0; i < _battleInfos.Count; i++)
			{
				BattleInfo battleInfo = _battleInfos[i];
				_lockStatusMap[battleInfo.id] = battleInfo.defaultLock;
			}
			_isLoaded = true;
			/* string path = StreamTools.GetPersistentDataPath() + "battle.dat";
			if (File.Exists(path))
			{
				using (FileStream input = new FileStream(path, FileMode.Open))
				{
					BinaryReader reader = new BinaryReader(input);
					Load(reader);
				}
			} */
			string json = PlayerPrefs.GetString("battle.dat");
			BattleInfoList battleInfoList = JsonUtility.FromJson<BattleInfoList>(json);
			if (battleInfoList != null)
			{
				foreach (var item in battleInfoList.list)
				{
					_lockStatusMap[item.key] = item.value;
				}
			}
		}
	}

	public static bool IsLock(int id)
	{
		return _lockStatusMap[id];
	}

	public static void UnlockNextLevel(int id)
	{
		int key = id + 1;
		if (_lockStatusMap.ContainsKey(key))
		{
			_lockStatusMap[key] = false;
		}
		Save();
	}

	private static void Save(BinaryWriter writer)
	{
		writer.Write(_lockStatusMap.Count);
		foreach (KeyValuePair<int, bool> item in _lockStatusMap)
		{
			writer.Write(item.Key);
			writer.Write(item.Value);
		}
	}

	private static void Load(BinaryReader reader)
	{
		int num = reader.ReadInt32();
		for (int i = 0; i < num; i++)
		{
			int key = reader.ReadInt32();
			bool value = reader.ReadBoolean();
			_lockStatusMap[key] = value;
		}
	}

	private static void Save()
	{
		BattleInfoList battleInfoList = new BattleInfoList();
		foreach (KeyValuePair<int, bool> item in _lockStatusMap)
		{
			battleInfoList.list.Add(new BattleInfoValue(item.Key, item.Value));
		}
		string json = JsonUtility.ToJson(battleInfoList);
		PlayerPrefs.SetString("battle.dat", json);
		PlayerPrefs.Save();
		/* string text = StreamTools.GetPersistentDataPath() + "battle.dat";
		string path = text.Substring(0, text.LastIndexOf('/'));
		if (!Directory.Exists(path))
		{
			Directory.CreateDirectory(path);
		}
		using (FileStream output = new FileStream(text, FileMode.Create))
		{
			BinaryWriter writer = new BinaryWriter(output);
			Save(writer);
		} */
	}
}
